#!/usr/bin/env bash


source ./config/env.sh


yum -y install epel-release
yum -y install expect wget


function Config_SSH() {
    expect -c "
        set timeout -1;
        spawn ssh -o StrictHostKeyChecking=no $2 ${@:3};
        expect {
            *assword:* {send -- $1\r;
                        expect {
                            *denied* {exit 2;}
                            eof
                        }
            }
            eof     {exit 1;}
        }
    "
    return $?
}


# create /.ssh/
i=0
for node_ip in ${NODE_IP}
do
let i++
    Config_SSH `echo ${NODE_PASS} | cut -d " " -f $i` root@${node_ip} "test -e /root/.ssh && test -e /root/.ssh || mkdir /root/.ssh"
done

# generate keys
#ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa


# Configure key login
i=0
for node_ip in ${NODE_IP}
do
let i++
    `chmod +x ./download/expect_scp`
    ./download/expect_scp ${node_ip} root `echo ${NODE_PASS} | cut -d " " -f $i` ~/.ssh/id_rsa.pub /root/.ssh/authorized_keys
    echo -e "\n--ExitStatus:$?"
done

# The kernel optimization
cat > ./work/kubernetes.conf << EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0 # 禁止使用 swap 空间，只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=0
net.netfilter.nf_conntrack_max=2310720
EOF


#创建目录
i=0
for node_ip in ${NODE_IP}
do
let i++
   ssh root@${node_ip} "sudo mkdir -p ${K8S_BIN_DIR}"
   ssh root@${node_ip} "sudo mkdir -p ${K8S_DATA}"
   ssh root@${node_ip} "sudo mkdir -p ${K8S_CA_DIR}"
   ssh root@${node_ip} "sudo mkdir -p ${K8S_LOGS_DIR}"
done


# Initial installation
i=0
for node_ip in ${NODE_IP}
do
let i++
    ssh root@${node_ip} "sudo hostnamectl set-hostname `echo ${NODE_HOSTNAME} | cut -d " " -f $i`"
    ssh root@${node_ip} "sudo systemctl stop firewalld;systemctl disable firewalld"
    ssh root@${node_ip} "sudo iptables -F && sudo iptables -X && sudo iptables -F -t nat && sudo iptables -X -t nat && sudo iptables -P FORWARD ACCEPT"
    ssh root@${node_ip} "sudo swapoff -a;sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab "
    ssh root@${node_ip} "sudo setenforce 0;sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config"
    ssh root@${node_ip} "sudo systemctl stop dnsmasq;sudo systemctl disable dnsmasq"
    ssh root@${node_ip} "sudo yum clean all && sudo rm -fr /var/cache/yum && sudo yum makecache fast -y && sudo yum -y install epel-release"
    ssh root@${node_ip} "sudo mount -t cgroup -o cpu,cpuacct none /sys/fs/cgroup/cpu,cpuacct"
    ssh root@${node_ip} "sudo timedatectl set-timezone Asia/Shanghai"
    ssh root@${node_ip} "sudo timedatectl set-local-rtc 0"
    ssh root@${node_ip} "sudo systemctl restart rsyslog && sudo systemctl restart crond"
    ssh root@${node_ip} "sudo echo -e "* * * * * ntpdate ntp1.aliyun.com" > /var/spool/cron/root"
    scp ./work/kubernetes.conf root@${node_ip}:/etc/sysctl.d/kubernetes.conf
    ssh root@${node_ip} "sudo sysctl -p /etc/sysctl.d/kubernetes.conf"
done

# install package
for node_ip in ${NODE_IP}
do
    ssh root@${node_ip} "sudo yum -y install net-tools tree lrzsz git unzip zip libxml2-devel telnet vim sysstat gcc gcc-c++ cmake bind-utils wget git ntpdate bash-completion lsof strace openssl openssl-devel || sudo yum -y install net-tools tree git lrzsz unzip zip libxml2-devel telnet vim sysstat gcc gcc-c++ cmake bind-utils wget git ntpdate bash-completion lsof strace openssl openssl-devel"
    ssh root@${node_ip} "sudo yum -y install conntrack ipvsadm ipset jq sysstat curl iptables libseccomp curl nfs-utils || sudo yum -y install conntrack ipvsadm ipset jq sysstat curl iptables libseccomp curl nfs-utils"
done


# Load kernel module
for node_ip in ${NODE_IP}
do
    ssh root@${node_ip} "sudo modprobe br_netfilter"
    ssh root@${node_ip} "sudo modprobe ip_vs"
done


# Add hosts file
i=0
for node_ip in ${NODE_IP}
do
let i++
    echo "${node_ip} `echo ${NODE_HOSTNAME} | cut -d " " -f $i`" >> /etc/hosts
done

for node_ip in ${NODE_IP}
do
    scp /etc/hosts root@${node_ip}:/etc/hosts
done


# copy to master
source ./config/env.sh
for master_ip in ${MASTER_IP}
do
    scp /etc/hosts root@${master_ip}:/etc/hosts
done

for node_ip in ${NODE_IP}
do
    ssh root@${node_ip} "systemctl start rpcbind"
done

# The kernel upgrade
#for node_ip in ${NODE_IP}
#do
#    ssh root@${node_ip} "sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm || sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm"
#    ssh root@${node_ip} "sudo yum --enablerepo=elrepo-kernel install -y kernel-lt || sudo yum --enablerepo=elrepo-kernel install -y kernel-lt"
#    ssh root@${node_ip} "sudo grub2-set-default 0"
#done
